#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# Author: Jeokayne Ataier

import threading as td
import time
import multiprocessing as mp


def job(q):
    res = 0
    for i in range(1000000):
        res += i + i**2 + i**3
    q.put(res)
def multicore():
    q = mp.Queue()
    m1 = mp.Process(target=job,args=(q,))
    m2 = mp.Process(target=job,args=(q,))
    m1.start()
    m2.start()
    m1.join()
    m2.join()
    r1 = q.get()
    r2 = q.get()
    print("multicore result:",r1+r2)


def multithreading():
    q = mp.Queue()
    t1 = td.Thread(target=job,args=(q,))
    t2 = td.Thread(target=job,args=(q,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    r1 = q.get()
    r2 = q.get()
    print("mutithreading result:",r1+r2)


def nomal():
    res = 0
    for j in range(2):
        for i in range(1000000):
            res += i + i ** 2 + i ** 3
    print(res)
if __name__ == "__main__":
    start_time1 = time.time()
    nomal()
    stop_time1 = time.time()
    print("nomal time:",stop_time1-start_time1)
    start_time1 = time.time()
    multithreading()
    stop_time1 = time.time()
    print("multithreading time:",stop_time1-start_time1)
    start_time1 = time.time()
    multicore()
    stop_time1 = time.time()
    print("multicore time:",stop_time1-start_time1)


'''
输出结果 数据量大的情况下，多核快：

499999666667166666000000
nomal time: 1.4791746139526367
mutithreading result: 499999666667166666000000
multithreading time: 1.5039806365966797
multicore result: 499999666667166666000000
multicore time: 0.9778776168823242


'''